#ifndef   __MOTOR_H
#define   __MOTOR_H

#include "SVPWM.h"
#include "SC60228.h"

struct PIStruct
{
	int16_t Ref;
	int16_t Feedback;
	int16_t Err;
	int16_t Kp;
	int16_t Proportion;
	int16_t Ki;
	int32_t Integer;
	int16_t Kc;	
	int16_t Excess;
	int16_t Limite;
	int16_t Out;
};

struct MOTORCON_S
{
    unsigned char En;   
    long ThetaM;        
    long ThetaM_Offset; 
    long PPN;           
    long ThetaE;       
    long Speed;  
    unsigned char Debug;
    unsigned char Debug_State;
    long Debug_Count; 
    unsigned char NEG;  
    long IA;            
    long IB;          
    long I_Gain;       
    long Ialfa;
    long Ibeta;
    long Id;           
    long Iq;            
    long Idref;        
    long Iqref;        
    long Kp;         
    long Ki;          
	long IdInteger;    
    long IqInteger;    
    long Ud;
    long Uq;
    long Ualfa;
    long Ubeta;
	long VA;
	long VB;
	long VC;
    long Tperiod;      
    long Comp1;
    long Comp2;
    long Comp3;
};

void Motor_Init(void);
void Motor_Open_Loop_Control(struct MOTORCON_S *Motor);
void Motor_Control(void);
void PLL_Speed_Run(float phase, float *phase_var, float *speed_var);
int16_t Motor_Speed_Control(int32_t ref, int32_t feed, float *Integer, float Pk, float Ik);

extern struct MOTORCON_S Motor;
extern int16_t Motor_Iq_Set, Motor_Id_Set;
extern int16_t Motor_Iq_Set_Max, Motor_Id_Set_Max;
extern float PLL_Angle, PLL_Speed;
extern int32_t Speed_Ref;
extern int32_t RPM_Ref, RPM_Set, RPM_Step;   
extern float Speed_P, Speed_I;
extern float Speed_Integer;
extern uint16_t Time_Count_Gap;
extern int16_t Angle_Add;

#endif
